<?php

namespace backend\services\active;

use backend\extensions\ApiBaseService;
use backend\models\active\ActiveCouponReceive;

/**
 * Desc 优惠券领取服务类
 * @author WMX
 */
class ActiveCouponReceiveService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        //检索条件
        $condition = ['AND'];
        if ($params['name']) {
            $condition[] = ['like', 'ac.name', $params['name']??''];
        }

        $list = ActiveCouponReceive::getUnionAll($condition, $params['page'], $params['page_size'], '', ['ac.name', 'ac.denomination','ac.type','ac.desc','ac.threshold', 'acr.*']);
        foreach($list['list'] as &$value) {
            $value['state_name'] = ActiveCouponReceive::$state[$value['state']] ?? '';
            $value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
            $value['end_time'] = date('Y-m-d H:i:s', $value['end_time']);
        }
        return $list;
    }


    // 作废
    public static function cancel($params)
    {
        $ac_info = ActiveCouponReceive::getDataOne(['id'=> $params['id']]);
        if(empty($ac_info)) {
            return self::jsonError('参数异常');
        }

        if($ac_info['state'] != ActiveCouponReceive::STATE_INI) {
            return self::jsonError('不是待使用，不支持作废处理');
        }

        $res = ActiveCouponReceive::updateData(['state'=> ActiveCouponReceive::STATE_CANCEL],['id'=> $params['id']]);
        if (empty($res)) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess([]);
        
    }


}
